<?php
class Imagecode
{
    private $width;
    private $height;
    private $counts;
    private $distrubcode;
    private $fonturl;
    private $fonturlbg;
    private $session;

    /**
     * __construct
     *
     * @param int $width 宽度
     * @param int $height 高度
     * @param int $counts 字符个数
     * @param string $distrubcode 随机字符范围
     * @param string $fonturl 严重码字体
     * @param string $fonturlbg 干扰字符字体
     */
    function __construct($width = 120, $height = 30, $counts = 5,
                         $distrubcode = "1235467890qwertyuipkjhgfdaszxcvbnm",
                         $fonturl = "1.ttf", $fonturlbg = '2.ttf')
    {
        $this->width = $width;
        $this->height = $height;
        $this->counts = $counts;
        $this->distrubcode = $distrubcode;
        $this->fonturl = FRAMEWORK_PATH . '/Helper/' .$fonturl;
        $this->fonturlbg = $fonturlbg ? FRAMEWORK_PATH . '/Helper/' .$fonturlbg : FRAMEWORK_PATH . '/Helper/' .$fonturl; //未设置干扰字体时使用
        $this->session = $this->sessioncode();
        //session_start();
        $_SESSION['code'] = $this->session;
    }

    function imageout()
    {
        $im = $this->createimagesource();
        $this->setbackgroundcolor($im);
        $this->set_code($im);
        $this->setdistrubecode($im);
        ImageGIF($im);
        ImageDestroy($im);
    }

    private function createimagesource()
    {
        return imagecreate($this->width, $this->height);
    }

    private function setbackgroundcolor($im)
    {
        $bgcolor = ImageColorAllocate($im, rand(200, 255), rand(200, 255), rand(200, 255)); //±³¾°ÑÕÉ«
        imagefill($im, 0, 0, $bgcolor);
    }

    private function setdistrubecode($im)
    {
        $count_h = $this->height;
        $cou = floor($count_h * 2);
        for ($i = 0; $i < $cou; $i++) {
            $x = rand(0, $this->width);
            $y = rand(0, $this->height);
            $jiaodu = rand(0, 360);
            $fontsize = rand(4, 10);
            $fonturl = $this->fonturlbg;
            $originalcode = $this->distrubcode;
            $countdistrub = strlen($originalcode);
            $dscode = $originalcode[rand(0, $countdistrub - 1)];
            $color = ImageColorAllocate($im, rand(40, 140), rand(40, 140), rand(40, 140)); //干扰文字颜色
            imagettftext($im, $fontsize, $jiaodu, $x, $y, $color, $fonturl, $dscode);
        }
    }

    private function set_code($im)
    {
        $width = $this->width;
        $counts = $this->counts;
        $height = $this->height;
        $scode = $this->session;
        $y = floor($height / 2) + floor($height / 4);
        $fontsize = rand(30, 35);
        $fonturl = $this->fonturl;

        $counts = $this->counts;
        for ($i = 0; $i < $counts; $i++) {
            $char = $scode[$i];
            $x = floor($width / $counts) * $i + 8;
            $jiaodu = rand(-20, 30);
            $color = ImageColorAllocate($im, rand(0, 200), rand(50, 200), rand(100, 240)); //文字颜色
            imagettftext($im, $fontsize, $jiaodu, $x, $y, $color, $fonturl, $char);
        }
    }

    private function sessioncode()
    {
        $originalcode = $this->distrubcode;
        $countdistrub = strlen($originalcode);
        $_dscode = "";
        $counts = $this->counts;
        for ($j = 0; $j < $counts; $j++) {
            $dscode = $originalcode[rand(0, $countdistrub - 1)];
            $_dscode .= $dscode;
        }
        return $_dscode;
    }
}
/*
 * Header("Content-type: image/GIF");
 * $imagecode=new Imagecode(160,50);
 * $imagecode->imageout();
 */

